package com.cjkj.auth.controller;

import com.cjkj.auth.form.AppLoginBody;
import com.cjkj.auth.service.AppLoginService;
import com.cjkj.common.core.domain.R;
import com.cjkj.common.core.utils.StringUtils;
import com.cjkj.common.security.auth.AuthUtil;
import com.cjkj.common.security.service.AppletTokenService;
import com.cjkj.common.security.utils.SecurityUtils;
import com.cjkj.system.api.model.AppletLoginUser;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * APP账号登录
 */
@RestController
public class AppTokenController {

    @Resource
    private AppletTokenService appTokenService;
    @Resource
    private AppLoginService appLoginService;

    @PostMapping("appLogin")
    public R<?> login(@RequestBody AppLoginBody loginBody){
        AppletLoginUser login = appLoginService.login(loginBody);
        return R.ok(appTokenService.createToken(login));
    }
    @DeleteMapping("appLogout")
    public R<?> logout(HttpServletRequest request){
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            // 删除用户缓存记录
            AuthUtil.appLogoutByToken(token);
        }
        return R.ok("退出成功");
    }
}